/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package syferserver;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;

/**
 *
 * @author Yannick
 */
public class UDPStream extends Thread {

    private static DatagramSocket rs;
    public String ipa;
    //poort inkomende udp
    private int inport;
    //poort uitgaande udp
    public int uitport;
    public static Read r;
    public Write w = new Write();

    public int rport;
    public String rip;

    /**
     * Server side
     * lezen en schrijven starten
     * @param inport = poort waarop server udp pakketen ontvangt
     */
    public UDPStream(String hostname, int inport, int uitport) {
        ipa = hostname;
        this.inport = inport;
        this.uitport = uitport;
        try {
            if (rs == null) {
                rs = new DatagramSocket(inport);
            }
            if (r == null) {
                r = new Read();
                r.start();
            }
            w.start();
            System.out.println("Server udp: reading / writing started");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Client side udpstream
     * lezen en schrijven starten
     * @param hostname = VoIP Server
     * @param port = poort op naar te schrijven op server
     */
    public UDPStream(String hostname, int port) {
        ipa = hostname;
        try {
            rs = new DatagramSocket();
            r = new Read();
            r.start();
            w.start();
            this.uitport = port;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Client-side
     * @return Poort waarop gelezen wordt
     */
    public static int getLocalPort(){
        return rs.getLocalPort();
    }

    public UDPStream(int port, boolean b) {
        try {
            if (rs == null) {
                rs = new DatagramSocket(port);
            }
            if (r == null) {
                r = new Read();
                r.start();
            }
            w.start();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public class Write extends Thread {

        public void write(byte[] tempBuffer) {
            try {

                DatagramPacket p = new DatagramPacket(tempBuffer, 1024, InetAddress.getByName(ipa), uitport);
                rs.send(p);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public class Read extends Thread {

        public boolean read(byte[] tempBuffer) {
            try {
                DatagramPacket p = new DatagramPacket(tempBuffer, 1024);
                rs.receive(p);

                rip=p.getAddress().getHostAddress();
                rport =p.getPort();

                tempBuffer = p.getData();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return true;
        }
    }
}
